<template>
  <div class="my-container">
    <!-- 头部已登录 -->
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <div class="left">
          <van-image
            class="avatar"
            lazy-load
            round
            fit="cover"
            :src="userInfo.photo"
          />
          <span class="nickname">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button round size="mini" to="user-profile">编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <div class="count">{{ userInfo.art_count }}</div>
          <div class="text">头条</div>
        </div>
        <div class="data-item">
          <div class="count">{{ userInfo.follow_count }}</div>
          <div class="text">关注</div>
        </div>
        <div class="data-item">
          <div class="count">{{ userInfo.fans_count }}</div>
          <div class="text">粉丝</div>
        </div>
        <div class="data-item">
          <div class="count">{{ userInfo.like_count }}</div>
          <div class="text">获赞</div>
        </div>
      </div>
    </div>

    <!-- 头部未登录 -->
    <div class="header not-login" v-else>
      <div class="login-btn" @click="$router.push('/login')">
        <img src="~@/assets/mobile.png" class="mobile-img" />
        <span class="text">登录 ☢ 注册</span>
      </div>
    </div>

    <!-- 宫格导航(收藏历史) -->
    <van-grid :column-num="3" class="grid-nav" clickable>
      <van-grid-item class="grid-item">
        <template #icon><i class="toutiao toutiao-shoucang"></i></template>
        <template #text> <span class="text">收藏</span></template>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <template #icon><i class="toutiao toutiao-lishi"></i></template>
        <template #text> <span class="text">历史</span></template>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <template #icon><i class="toutiao toutiao-zuopin"></i></template>
        <template #text> <span class="text">作品</span></template>
      </van-grid-item>
    </van-grid>

    <!-- 单元格导航 -->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell
      v-if="user"
      @click="onLogout"
      clickable
      class="logout-cell"
      title="退出登录"
      center
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'MyIndex',
  data() {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    async onLogout() {
      // 退出提示
      try {
        await this.$dialog.confirm({ title: '确认退出码' })
        this.$store.commit('setUser', null)
      } catch (err) {
        console.log('取消')
      }
    },
    async loadUserInfo() {
      try {
        const { data: res } = await this.$api.getUserInfo()
        console.log(res)
        this.userInfo = res.data
      } catch (err) {
        this.$toast('获取数据失败,请稍后重试')
      }
    }
  },
  created() {
    // 如果用户登陆了,则请求加载用户信息
    if (this.user) this.loadUserInfo()
  }
}
</script>

<style lang="less" scoped>
.my-container {
  // 头部
  .header {
    height: 361px;
    background: url('~@/assets/banner.png');
    background-size: cover;
  }
  // 头部未登录
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
    }
    .mobile-img {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
    }
    .text {
      font-size: 28px;
      color: #fff;
    }
  }
  // 头部已登录
  .user-info {
    .base-info {
      box-sizing: border-box;
      height: 231px;
      padding: 76px 32px 23px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .nickname {
          font-size: 30px;
          color: #fff;
        }
        .avatar {
          margin-right: 23px;
          width: 132px;
          height: 132px;
          border: 4px solid #fff;
        }
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        flex: 1;
        height: 130px;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  // 宫格导航(历史收藏)
  .grid-nav {
    .grid-item {
      height: 132px;
      i.toutiao {
        font-size: 45px;
      }
      .toutiao-shoucang {
        color: #eb5253;
      }
      .toutiao-lishi {
        color: #ff9d1d;
      }
      .toutiao-zuopin {
        color: deepskyblue;
      }
      .text {
        margin-top: 8px;
        font-size: 28px;
      }
    }
  }
  // 单元格导航
  .logout-cell {
    height: 100px;
    text-align: center;
    color: #eb5253;
    margin: 10px 0;
    font-size: 30px;
  }
}
</style>
